<?php


namespace App\Helpers;


use App\Models\Car\CarStatistic;
use App\Models\Order;
use Illuminate\Support\Carbon;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\ForbidLeaseDate;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Yansongda\Pay\Pay;
use function AlibabaCloud\Client\json;

trait PayTrait
{
    public function WxPay(){
        $config = config('pay.wechat');
        return Pay::wechat($config);

    }
    //自定义商户号
    public function outTrade($pay_type){
        $outtradeno=$pay_type.date('YmdHis').mt_rand(100,999);
        $paylog=Order::where('out_trade_no',$outtradeno)->count();
        if($paylog){
            $outtradeno=$pay_type.date('YmdHis').mt_rand(100,999);
        }
        return $outtradeno;
    }
    //微信扫码支付
    public function wxscan($data){
        $config = config('pay');
        Pay::config($config);
        $price = round($data->bc_total,2)*100;
        if($price<=0){
            $price = 1;
        }
        $order = [
            'out_trade_no' => time().'123',
            'description' => 'subject-测试',
            'amount' => [
                'total' => $price,
            ],
            'notify_url'=>env('APP_URL').'/api/pay/web_wx_notify'
        ];
        //return $order;
        $result = Pay::wechat()->scan($order);
        return $result;
    }
    //v2小程序支付
    public function miniv2Data($data){
        $config = config('pay');
        Pay::config($config);
        $price = round($data->bc_total,2)*100;
        if($price<=0){
            $price = 1;
        }
        $order = [
            'out_trade_no' => $data->order_sn,
            'description'=>'小程序订单',
            'amount' => [
                'total' => $price,
                'currency' => 'CNY',
            ],
            'payer' => [
                'openid' => $data->openid,
            ],
        ];
        return $result = Pay::wechat()->mini($order);
    }
    //小程序支付
    public function miniData($data){
        $price = round($data->bc_total,2)*100;
        if($price<=0){
            $price = 1;
        }
        $order = [
            'out_trade_no' => $data->out_trade_no,
            'body' => 'zc：'.$data->out_trade_no,
            'total_fee' =>$price,
            'openid'=>$data->openid,
        ];
        return $this->WxPay()->miniapp($order);
    }
    //小程序退款
    public function miniRefund($order_sn,$out_refund_no,$price,$total){
        $config = config('pay');
        Pay::config($config);
        $order = [
            'out_trade_no' => $order_sn,
            'out_refund_no' => $out_refund_no,
            'amount' => [
                'refund' => $price,
                'total' => $total,
                'currency' => 'CNY',
            ],
        ];
        $result = Pay::wechat()->refund($order);
        info('退款结果'.json_encode($result));
    }

    //设置当前车辆状态
    public function changeCarStatus($id,$status){
        info('维修订单'.$id);
        RecognizeDrivingLicense::query()
            ->where('id',$id)
            ->where('use_status',7)
            ->update(['use_status'=>$status,'is_used'=>1]);
    }
    //更改车辆年检相关状态
    protected function changeLimitYear($id,$where,$value){
        RecognizeDrivingLicense::query()
            ->where('id',$id)->update([$where=>$value]);
    }
    //禁止排车时间安排
    protected function forbidDate($need_start,$need_end,$car_id,$status,$car_series_id){
        $days = $this->setForbidDate($need_start,$need_end);
        if(count($days)>0){
            foreach ($days as $v){
                $data[]=[
                    'car_series_id'=>$car_series_id,
                    'month'=>now()->format('Ym'),
                    'non_lease_date'=>$v->format('Y/m/d'),
                    'need_start'=>$need_start,
                    'need_end'=>$need_end,
                    'forbid_date'=>$v->format('Ymd'),
                    'car_id'=>$car_id,
                    'status'=>$status,
                ];
            }
            ForbidLeaseDate::query()
                ->insert($data);
        }

    }
    //解除排车禁止时间
    protected function confirmForbidDate($car_id,$status){
        ForbidLeaseDate::query()
            ->where('car_id',$car_id)
            ->where('status',$status)
            ->delete();
    }
    //设置禁用车辆时间
    protected function setForbidDate($start, $end){
        $days = $this->getForbidDays($start, $end);
        $array = [];
        for($i=0;$i<$days;$i++){
            $array[] = Carbon::make($start)->addDays($i);
        }
        return $array;
    }
    protected function getForbidDays($start, $end){
        $days = Carbon::parse($start)->diffInDays(Carbon::parse($end));
        $second = strtotime($end) - strtotime($start);//$this->diffSeconds($end, $start);
        $day = 3600 * 24 * $days;
        $second = $second - $day;
        if ($second >0) {
            return $days + 1;
        }else {
            return $days;
        }
    }
    //求出改价功能
    protected function changeOrder($order_id,$change_price=0,$deposit=0,$break_rules=0){
        $order = CarOrder::query()->find($order_id);
        if($change_price>0){
            $total_deposit = $order->deposit+$order->break_rules;//押金费用
            $order->bc_total = $order->change_price+$total_deposit;
            $order->change_price = $change_price;
            $order->status = 3;
            $order->save();
        }
        if($deposit>=0){
            if($order->change_price>0){
                $price = $order->change_price;
            }else{
                $price = $order->bc_service_total+$order->bc_base_price_total+$order->bc_brokerage_total+$order->bc_lease_price_total;
            }
            $total_deposit = $deposit+$order->break_rules;//押金费用
            $order->bc_total = $price+$total_deposit;
            $order->save();
        }
        if($break_rules>=0){
            if($order->change_price>0){
                $price = $order->change_price;
            }else{
                $price = $order->bc_service_total+$order->bc_base_price_total+$order->bc_brokerage_total+$order->bc_lease_price_total;
            }
            $total_deposit = $order->deposit+$break_rules;//押金费用
            $order->bc_total = $price+$total_deposit;
            $order->save();
        }
    }
//    是否加班
    public function isWorkOvertime(){
        $now_num = now()->format('Hi');
        if($now_num>1800||$now_num<730){
            return 1;
        }else{
            return 0;
        }
    }
    // 加入统计
    public function statisticsOrder($order){
        $static = CarStatistic::query()
            ->where('car_id',$order->car_id)->first();
        if(!$static){
            $data = [
                'car_id'=>$order->gear_car_id,
                'series_name'=>$order->gear_car_name,
                'lease_times'=>1,
                'lease_days'=>$order->days,
                'total_amount'=>$order->bc_lease_price_total,
                'total_lease_amount'=>$order->bc_lease_price_total,
            ];
            CarStatistic::query()->create($data);
        }else{
            $static->lease_times = $static->lease_times+1;
            $static->lease_days = $static->lease_days+$order->days;
            $static->total_amount = $static->total_amount+$order->bc_lease_price_total;
            $static->total_lease_amount = $static->total_lease_amount+$order->bc_lease_price_total;
            $static->save();
        }

    }
}
